import { history } from 'umi'
import qs from 'qs';
// import { isArray } from 'util';

const routerReplace = ({ query }) => {
    const { pathname, search } = window.location;
    const currentQuery = qs.parse(search, { ignoreQueryPrefix: true });

    const finalQuery = { ...currentQuery };
    Object.keys(query).forEach(key => {
        // if (!query[key]) {
        //     return;
        // }
        if (Array.isArray(query[key])) {
            if (query[key].length !== 0) {
                finalQuery[key] = query[key].join(',');
            }
            return;
        }
        finalQuery[key] = query[key];
    });

    history.push({ pathname, query: finalQuery });
};

export default routerReplace;
